<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="sj" uri="/struts-jquery-tags"%>
<%@ taglib prefix="sjg" uri="/struts-jquery-grid-tags"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<s:url id="loanappurl" value="loanprofile-loanapp.jsp" />
<script type="text/javascript">
    var rowId = 0;
    
    $('.ui-reset , #refresh_loanappliGrid').click(function(){
        $("#loanappliGrid").jqGrid('setGridParam',{search:false});
        var postData = $("#loanappliGrid").jqGrid('getGridParam','postData');
        $.extend(postData,{searchField:"",searchString:"",searchOper:""});
        $.extend(postData,{filters:""});
        $("#loanappliGrid").trigger("reloadGrid");
    });

    $.subscribe('loanappliGridRowSelected', function(event,data) {
        rowId = event.originalEvent.id;
        $( "#loanappliGrid_edit" ).button( "option", "disabled", false );
        $( "#loanappliGrid_delete" ).button( "option", "disabled", false );
    });

    $.subscribe('searchloanappliGrid', function(event,data) {
        $("#loanappliGrid").jqGrid('searchGrid', {drag:true,closeAfterSearch:true,closeOnEscape:true,sopt:['eq'],caption:"Search loanappli"} );
    });

    $.subscribe('addloanappliGrid', function(event,data) {
        $("#loanappliGrid").jqGrid('editGridRow',"new",{drag:true,closeAfterEdit:false,closeOnEscape:true,jqModal:true,width:350,left:310,top:20,reloadAfterSubmit:true,position:"last",afterSubmit:function(response, postdata) {return isError(response.responseText);}});
    });

    $.subscribe('editloanappliGrid', function(event,data) {
        if(rowId!=0){
            $("#loanappliGrid").jqGrid('editGridRow',rowId,{drag:true,closeAfterEdit:true,closeOnEscape:true,jqModal:true,width:350,left:310,top:20,reloadAfterSubmit:true,afterSubmit:function(response, postdata) {return isError(response.responseText);}});
        }else{
            alert("Sorry. Please select an loanappli first.");
        }
    });

    $.subscribe('deleteloanappliGrid',function(event,data){
        if(rowId!=0){
            $("#loanappliGrid").jqGrid('delGridRow',rowId,{drag:true,closeOnEscape:true,closeAfterEdit:true,jqModal:true,left:310,top:20,reloadAfterSubmit:true});
        }else{
            alert("Sorry. Please select an loanappli first.");
        }
    });
    
    $.subscribe('loanappliGridComplete',function(event,data){
        $( "#loanappliGrid_search" ).button( "option", "disabled", false );
        $( "#loanappliGrid_add" ).button( "option", "disabled", false );
    });
    
    function isError(text) {
        if(text.indexOf('ERROR') >= 0) { return [false, text]; }
        return [true,'Done.'];
    }

    function lengthCheck(value, colname) {
        if (value.length < 6 || value.length >11)
            return [false," Id Number must be between 6 to 11 digits only."];
        else
            return [true,""];
    }
    
    function formatLink(cellvalue, options, rowObject) {
        return "<a href='<s:property value="loanappurl" />?id="+rowObject["loanverificationid"]+"' onClick='javascript: return openDialog(this)'>" + cellvalue + "</a>";
    }
    function openDialog(e) {
        window.open(e.href, "newWindow");
        return false;
    }
</script>
<div class="gridOptsContainer">
</div>
<c:if test="${param.cat == 'posted'}">
    <%--<% System.out.println("POSTED"); %>--%>
    <s:url id="remoteurl" action="grid-loanappli-posted"/>
</c:if>
<c:if test="${param.cat == 'saved'}">
    <%--<% System.out.println("SAVED"); %>--%>
    <s:url id="remoteurl" action="grid-loanappli-saved"/> 
</c:if>    


<s:url id="editurl" action="edit-loanappli"/>
<s:url id="selectOptionsUsertypes" action="select-usertypes"/>
<s:url id="selectOptionsBranches" action="select-branches"/>
<sjg:grid
    id="loanappliGrid"
    caption="Cashmate loanapps"
    dataType="json"
    href="%{remoteurl}"
    editurl="%{editurl}"
    gridModel="gridModel"
    groupField="['areaname']"
    groupColumnShow="[true]"
    groupCollapse="false"
    groupText="['<b>{0} - {1} loanapp(s)</b>']"
    pager="true"
    pagerButtons="true"
    pagerInput="true"
    shrinkToFit="false"
    rowList="10,20,50"
    rowNum="10"
    loadingText="Requesting Data..."
    width="970"
    hoverrows="true"
    rownumbers="true"
    autoencode="true"
    viewrecords="true"
    navigator="true"
    navigatorAdd="false"
    navigatorDelete="false"
    navigatorEdit="false"
    navigatorSearch="false"
    navigatorView="true"
    onSelectRowTopics="loanappGridRowSelected"
    onGridCompleteTopics="loanappGridComplete"
    >

    <sjg:gridColumn name="loanverificationid"
                    key="true"
                    index="loanverificationid"
                    title="Loan Application ID (Not Editable)"
                    sortable="false"
                    hidden="true"
                    align="center"
                    />

    <sjg:gridColumn name="documentno"
                    index="documentno"
                    title="Doc No."
                    sortable="true"
                    formatter = "formatLink"
                    editrules="{required:true}"
                    editable="true"
                    editoptions="{maxlength:'45'}"
                    edittype="text"
                    search="true"
                    searchoptions="{sopt:['eq']}"
                    align="center"
                    />

    <sjg:gridColumn name="borrowername"
                    index="borrowername"
                    title="Borrower Name"
                    sortable="true"
                    editrules="{required:true}"
                    editable="true"
                    editoptions="{maxlength:'45'}"
                    edittype="text"
                    search="true"
                    searchoptions="{sopt:['eq']}"
                    align="center"
                    />

    <sjg:gridColumn name="branchname"
                    index="branchname"
                    title="Branch"
                    sortable="false"
                    search="true"
                    editable="true"
                    searchoptions="{sopt:['eq']}"
                    edittype="select"
                    editoptions="{dataUrl : '%{selectOptionsBranches}'}"
                    align="center"
                    />

    <sjg:gridColumn name="areaname"
                    index="areaname"
                    title="Area"
                    sortable="false"
                    search="true"
                    editable="true"
                    searchoptions="{sopt:['eq']}"
                    edittype="select"
                    editoptions="{dataUrl : '%{selectOptionsBranches}'}"
                    align="center"
                    />

    <sjg:gridColumn name="status"
                    index="status"
                    title="Status"
                    search="true"
                    searchoptions="{sopt:['eq']}"
                    sortable="true"
                    align="center"
                    />

    <sjg:gridColumn name="loantype"
                    index="loantype"
                    title="Loan Type"
                    sortable="true"
                    editable="true"
                    edittype="text"
                    search="true"
                    editrules="{number:true,required:true,custom:true,custom_func:lengthCheck}"
                    searchoptions="{sopt:['eq']}"
                    editoptions="{maxlength:'11'}"
                    align="center"
                    />

    <sjg:gridColumn name="loanterm"
                    index="loanterm"
                    title="Loan Term"
                    sortable="true"
                    editoptions="{maxlength:'45'}"
                    editrules="{required:true}"
                    editable="true"
                    edittype="text"
                    search="true"
                    searchoptions="{sopt:['eq']}"
                    align="center"
                    />

    <sjg:gridColumn name="amountapplied"
                    index="amountapplied"
                    title="Amount Applied"
                    search="false"
                    editable="true"
                    edittype="text"
                    editoptions="{maxlength:'45'}"
                    editrules="{required:true}"
                    align="center"
                    />

    <sjg:gridColumn name="documentdate"
                    index="documentdate"
                    title="LA Date"
                    search="false"
                    editable="true"
                    edittype="text"
                    editoptions="{maxlength:'45'}"
                    editrules="{required:true}"
                    align="center"
                    />

    <sjg:gridColumn name="creditrating"
                    index="creditrating"
                    title="Credit Rating"
                    search="true"
                    searchoptions="{sopt:['eq']}"
                    sortable="true"
                    align="center"
                    />

    <sjg:gridColumn name="amountofstocks"
                    index="amountofstocks"
                    title="Amount of Stocks"
                    search="true"
                    searchoptions="{sopt:['eq']}"
                    sortable="true"
                    align="center"
                    />

    <sjg:gridColumn name="businesstype"
                    index="businesstype"
                    title="Business Type"
                    search="true"
                    searchoptions="{sopt:['eq']}"
                    sortable="true"
                    align="center"
                    />

    <sjg:gridColumn name="coborrowername"
                    index="coborrowername"
                    title="Coborrower Name"
                    search="true"
                    searchoptions="{sopt:['eq']}"
                    sortable="true"
                    align="center"
                    />

    <sjg:gridColumn name="withaf"
                    index="withaf"
                    title="With AF"
                    search="true"
                    searchoptions="{sopt:['eq']}"
                    sortable="true"
                    align="center"
                    />

    <sjg:gridColumn name="withci"
                    index="withci"
                    title="With CI"
                    search="true"
                    searchoptions="{sopt:['eq']}"
                    sortable="true"
                    align="center"
                    />

    <sjg:gridColumn name="withap"
                    index="withap"
                    title="With AP"
                    search="true"
                    searchoptions="{sopt:['eq']}"
                    sortable="true"
                    align="center"
                    />

    <sjg:gridColumn name="withcm"
                    index="withcm"
                    title="With CM"
                    search="true"
                    searchoptions="{sopt:['eq']}"
                    sortable="true"
                    align="center"
                    />

</sjg:grid>
